# coding=utf-8
import os
import cv2
import numpy as np


def one_hot(str_arr, num_classes):
    label = np.zeros(num_classes, dtype=np.int32)
    idx = [int(i) for i in str_arr]
    label[idx] = 1

    return label

def parse_classify_line(line, num_classes, image_root=""):
    if isinstance(line, list):
        text = line
    else:
        text = line.strip().split()
    img_dir = os.path.join(image_root, text[0])
    
    image = cv2.imread(img_dir, cv2.IMREAD_COLOR)
    label = one_hot(text[1:], num_classes)

    return image, label

def dir2lab(idir, delim='/'):
    try:
        idir = os.path.abspath(idir)
        lab = idir.split(delim)[-2]
    except:
        lab = ''
    return lab
